/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
package com.joolun.web.api;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.mall.entity.Chain;
import com.joolun.mall.entity.ChainOrder;
import com.joolun.mall.service.IChainOrderService;
import com.joolun.mall.service.IChainService;
import com.joolun.weixin.utils.ThirdSessionHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 * 用户接龙接口
 * @author JL
 * @date 2019-09-10 15:21:22
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/api/ma/chain")
@Api(value = "chain", tags = "接龙api")
public class ChainApi {

	@Autowired
	private IChainService chainService;


	@Autowired
	private IChainOrderService chainOrderService;

//	@Autowired
//	private RedisonClient redisClient;

	/**
	 * 接龙详情
	 * @param chainId 接龙id
	 * @return AjaxResult
	 */
	@ApiOperation(value = "接龙详情")
	@GetMapping("detail/{chainId}")
	public AjaxResult<Chain> userLink(@PathVariable("chainId") Long chainId){
		return AjaxResult.success(chainService.detail(chainId,true));
	}

	/**
	 * 接龙订单信息--获取上次提交的淘宝id
	 * @param
	 * @return AjaxResult
	 */
	@ApiOperation(value = "用户接龙提交")
	@PostMapping("lastTbId")
	public AjaxResult<String> lastTbId(){
		String lastTbId = chainOrderService.lastTbId();
		return new AjaxResult(200,"操作成功",lastTbId);
	}

	/**
	 * 接龙订单信息--新增
	 * @param chainOrder 接龙id
	 * @return AjaxResult
	 */
	@ApiOperation(value = "用户接龙提交")
	@PostMapping("chainOrder/add")
	public AjaxResult<Long> userLink(@RequestBody ChainOrder chainOrder){
        log.info("用户:{}接龙数据为:{}",ThirdSessionHolder.getThirdSession().getWxUserId(), JSONUtil.toJsonStr(chainOrder));
		return AjaxResult.success(chainOrderService.add(chainOrder));
	}

	/**
	 * 取消接龙订单
	 * @param chainOrderId 接龙订单id
	 * @return AjaxResult
	 */
	@ApiOperation(value = "用户取消接龙")
	@GetMapping("chainOrder/cancel/{chainOrderId}")
	public AjaxResult<Void> chainOrderCancel(@PathVariable("chainOrderId") Long chainOrderId){
		ChainOrder chainOrder = new ChainOrder();
		chainOrder.setId(chainOrderId);
		chainOrder.setUserId(Long.valueOf(ThirdSessionHolder.getThirdSession().getWxUserId()));
		LambdaUpdateWrapper<ChainOrder> wrapper = Wrappers.<ChainOrder>lambdaUpdate()
				.eq(ChainOrder::getUserId, chainOrder.getUserId())
				.eq(ChainOrder::getId, chainOrderId)
				.eq(ChainOrder::getDelFlag, "0")
				.set(ChainOrder::getStatus, "1");
		chainOrderService.update(wrapper);
		return AjaxResult.success();
	}

	/**
	 * 接龙订单信息--详情
	 * @param chainId 接龙id
	 * @return AjaxResult
	 */
	@ApiOperation(value = "用户接龙订单")
	@GetMapping("chainOrder/detail/{chainId}")
	public AjaxResult<ChainOrder> chainOrder(@PathVariable("chainId") Long chainId){
		String userId = ThirdSessionHolder.getThirdSession().getWxUserId();
		return AjaxResult.success(chainOrderService.chainOrder(chainId,userId));
	}

//	/**
//	 * 接龙订单信息--编辑
//	 * @param chainOrder 接龙订单信息
//	 * @return AjaxResult
//	 */
//	@ApiOperation(value = "修改订单信息")
//	@PutMapping("chainOrder/edit")
//	public AjaxResult<Void> chainOrderEdit(@RequestBody ChainOrder chainOrder){
//		String userId = ThirdSessionHolder.getThirdSession().getWxUserId();
//		chainOrder.setUserId(Long.valueOf(userId));
//		chainOrderService.update(chainOrder);
//		return AjaxResult.success();
//	}


}
